package com.sinog.biz.util;

import com.sinog.core.utilbean.Constants;
import org.apache.commons.lang.time.DateFormatUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @desc - 运维功能--统计个省份登录情况所需要用到的工具类，计算每月的工作日，这个Util需要每年手动维护
 * @author 潘志成
 * @date 2022/12/20 14:18
 */
public class WorkDayUtil {
    /**
     * 特殊的工作日(星期六、日工作)
     */
    private static final List<String> SPECIAL_WORK_DAYS = new ArrayList<>();

    /**
     * 特殊的休息日(星期一到五休息)
     */
    private static final List<String> SPECIAL_REST_DAYS = new ArrayList<>();

    static {
        initSpecialDays();
    }

    /**
     * 获取当前月份有几天是工作日
     * @param year 年
     * @param month 月
     * @return 工作日天数
     */
    public static int getDate(int year,int month) {
        String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Calendar calendar = Calendar.getInstance();
        // 不设置的话默认为当年
        calendar.set(Calendar.YEAR, year);
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 设置为当月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        // 当月最大天数
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int count = 0 ;
        for(int i = 0; i < daySize; i++) {
            if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                // 周六日如果上班就算1个工作日
                if(SPECIAL_WORK_DAYS.contains(DateFormatUtils.format(calendar.getTime(),"yyyy-MM-dd"))) {
                   count++;
                }
                calendar.add(Calendar.DATE,Constants.Number.NUM_ONE);
            } else {
                // 周1到周五休息就算1个休息日
                if(SPECIAL_REST_DAYS.contains(DateFormatUtils.format(calendar.getTime(),"yyyy-MM-dd"))) {
                    calendar.add(Calendar.DATE,Constants.Number.NUM_ONE);
                    continue;
                }else {
                    calendar.add(Calendar.DATE,Constants.Number.NUM_ONE);
                    count++;
                }
            }

        }
        return count;
    }

    /**
     * 判断当天是否是工作日
     * @param date 需要判断的日期
     * @return true-是工作日 false-不是工作日
     */
    public static boolean isWorkDay(String date) {
        Calendar calendar = Calendar.getInstance();
        //判断日期是否是周六周日
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
            //判断周六日是否是工作日
            if(SPECIAL_WORK_DAYS.contains(date)) {
                return true;
            }
            return false;
        }else {
            //判断周一至周五是否是休息日
            if(SPECIAL_REST_DAYS.contains(date)) {
                return false;
            }
            return true;
        }
    }

    /**
     * 手工维护特殊日(因为是未知的,所以必须手工维护)
     */
    private static void initSpecialDays() {
        SPECIAL_WORK_DAYS.add("2022-01-29");
        SPECIAL_WORK_DAYS.add("2022-01-30");
        SPECIAL_WORK_DAYS.add("2022-04-02");
        SPECIAL_WORK_DAYS.add("2022-04-24");
        SPECIAL_WORK_DAYS.add("2022-05-07");
        SPECIAL_WORK_DAYS.add("2022-10-08");
        SPECIAL_WORK_DAYS.add("2022-10-09");
        SPECIAL_WORK_DAYS.add("2023-01-28");
        SPECIAL_WORK_DAYS.add("2023-01-29");
        SPECIAL_WORK_DAYS.add("2023-05-06");
        SPECIAL_WORK_DAYS.add("2023-06-25");
        SPECIAL_WORK_DAYS.add("2023-10-07");
        SPECIAL_WORK_DAYS.add("2023-10-08");

        SPECIAL_REST_DAYS.add("2022-01-03");
        SPECIAL_REST_DAYS.add("2022-01-31");
        SPECIAL_REST_DAYS.add("2022-02-01");
        SPECIAL_REST_DAYS.add("2022-02-02");
        SPECIAL_REST_DAYS.add("2022-02-03");
        SPECIAL_REST_DAYS.add("2022-02-04");
        SPECIAL_REST_DAYS.add("2022-04-04");
        SPECIAL_REST_DAYS.add("2022-04-05");
        SPECIAL_REST_DAYS.add("2022-05-02");
        SPECIAL_REST_DAYS.add("2022-05-03");
        SPECIAL_REST_DAYS.add("2022-05-04");
        SPECIAL_REST_DAYS.add("2022-06-03");
        SPECIAL_REST_DAYS.add("2022-09-12");
        SPECIAL_REST_DAYS.add("2022-10-03");
        SPECIAL_REST_DAYS.add("2022-10-04");
        SPECIAL_REST_DAYS.add("2022-10-05");
        SPECIAL_REST_DAYS.add("2022-10-06");
        SPECIAL_REST_DAYS.add("2022-10-07");
        SPECIAL_REST_DAYS.add("2023-01-02");
        SPECIAL_REST_DAYS.add("2023-01-23");
        SPECIAL_REST_DAYS.add("2023-01-24");
        SPECIAL_REST_DAYS.add("2023-01-25");
        SPECIAL_REST_DAYS.add("2023-01-26");
        SPECIAL_REST_DAYS.add("2023-01-27");
        SPECIAL_REST_DAYS.add("2023-04-05");
        SPECIAL_REST_DAYS.add("2023-05-01");
        SPECIAL_REST_DAYS.add("2023-05-02");
        SPECIAL_REST_DAYS.add("2023-05-03");
        SPECIAL_REST_DAYS.add("2023-06-22");
        SPECIAL_REST_DAYS.add("2023-06-23");
        SPECIAL_REST_DAYS.add("2023-09-29");
        SPECIAL_REST_DAYS.add("2023-10-02");
        SPECIAL_REST_DAYS.add("2023-10-03");
        SPECIAL_REST_DAYS.add("2023-10-04");
        SPECIAL_REST_DAYS.add("2023-10-05");
        SPECIAL_REST_DAYS.add("2023-10-06");
    }
}